/*
 * Logger.cpp
 *
 *  Created on: Nov 28, 2008
 *      Author: brt
 */

#include <iostream>
#include <ctime>
#include <stdlib.h>

#include "../include/Logger.h"
#include "../../Matematica/include/Util.h"

/*
std::string logformat(const char * fmt, ...)
{
	const int max_size = 255;
	char buffer[max_size];

	va_list params;
	va_start(params, fmt);

	vsnprintf(buffer, max_size, fmt, params);

	va_end(params);

	return std::string(&buffer[0]);
}*/


std::string log_level_to_string(log_level_t l)
{
	switch (l)
	{
		case log_TRACE: return "trace";
		case log_DEBUG: return "debug";
		case log_INFO:  return "info";
		case log_WARN:  return "warn";
		case log_ERROR: return "error";
		case log_SCRIPT: return "script";

		default:        return "invalid";
	}
}

Logger::Logger()
	: m_logLevel()
{

}

std::ostream& Logger::Get(log_level_t level)
{
	m_logLevel = level;
	return m_buffer;
}

Logger::~Logger()
{
	time_t curr_time;
	std::time(&curr_time);
	struct tm * t = std::localtime(&curr_time);
	std::string log;
	LoggerBuffer& logger = LoggerBuffer::getInstance();


//	time_t current_time = tv.tv_sec;

	//gettimeofday();
/*
	if(m_logLevel == log_MILA)
	{
		log = m_buffer.str().c_str();
		logger.armazenarLogger(log);
		logger.imprimirLogger();

	}
	if (m_logLevel == log_DEBUG || m_logLevel == log_INFO)
	{
	    log = convert_to<std::string>(t->tm_hour) + ":"
            + convert_to<std::string>(t->tm_min) + ":"
            + convert_to<std::string>(t->tm_sec) + " - "
            + log_level_to_string(m_logLevel).c_str() + " - "
            + m_buffer.str().c_str();

        logger.armazenarLogger(log);
        logger.imprimirLogger();
	}*/

	if(m_logLevel == log_TRAINER)
	{

		log = m_buffer.str().c_str();
		logger.armazenarLogger(log);
		logger.imprimirLogger();

	}
//	else
//	{
//        fprintf(stderr, "%i:%i:%i - %s - %s\n", t->tm_hour,
//                t->tm_min,t->tm_sec,
//                log_level_to_string(m_logLevel).c_str(),
//                m_buffer.str().c_str());
//        fflush(stderr);
//	}
//	if (m_logLevel == log_SCRIPT)
//	{
//	    log = convert_to<std::string>(t->tm_hour) + ":"
//            + convert_to<std::string>(t->tm_min) + ":"
//            + convert_to<std::string>(t->tm_sec) + " - "
//            + log_level_to_string(m_logLevel).c_str() + " - "
//            + m_buffer.str().c_str();
//
//        logger.armazenarLogger(log);
//        logger.imprimirLogger();
//	}
}

/*static*/
LogParameters& LogParameters::instance()
{
	static LogParameters params;

	return params;
}

log_level_t LogParameters::level()
{
	return m_logLevel;
}

void LogParameters::level(log_level_t level)
{
	m_logLevel = level;
}

bool LogParameters::active(void)
{
	return m_active;
}

void LogParameters::active(bool isActive)
{
	m_active = isActive;
}
